/*
 * @Description: HTTP返回实体
 * @Author: Haor
 * @Date: 2022-04-02 23:17:34
 * @LastEditTime: 2022-05-09 11:13:08
 * @LastEditors: Haor
 */

/**
 * 枚举状态码
 */
export enum ResponseCode {
	SUCCESS = 1000, //表示已在响应中发出
	SERVER_ERROR = 1010, //通用服务器内部错误响应
	DATABASE_ERR = 1011, // 数据库异常
	UNKNOWN_ERROR = 1012, // 未知错误
	SIGN_ERROR = 1013, // 签名异常
	AUTHORITIES = 1020, //身份验证失败
	FORBIDDEN = 1021, //无权限
	PARAM_ERROR = 1031, // 参数异常
	OTHER = 204, //（无内容） - 资源有空表示
	FAILED = 1040, //操作失败
	NOT_FOUND = 1050,
	FILE_ERROR = 1042 // 文件读写错误
}

/**
 * 枚举状态对应的消息
 */
export enum Message {
	SUCCESS = `操作成功`,
	FAILED = `操作失败`,
	PARAMETER = `参数错误`,
	AUTHORITIES = `登录过期`,
	NOT_FOUND = '访问地址错误',
	FORBIDDEN = `无权访问`,
	SERVER_ERROR = `服务异常`
}

/**
 * 标准的返回模板
 */
export interface HttpResTemplate {
	code: number
	data?: any
	nonce?: number
	error?: any
	message: string
}

/**
 * 分页对象
 */
export interface Pager {
	list: any[]
	total: number
	limit?: number
	page?: number
}

/**
 * 标准分页的返回模板
 */
export interface HttpPageTemplate {
	code: number
	data: Pager
	message: string
}

/**
 * 返回数据实体对象
 */
class HttpResultClass {
	/**
	 * 成功返回结果
	 * @param message
	 * @param data
	 */
	success(data?: any, message?: string): HttpResTemplate {
		return {
			code: ResponseCode.SUCCESS,
			data,
			message: message || Message.SUCCESS
		}
	}

	/**
	 * 带分页的数据对象
	 * @param {*} msg
	 * @param {*} code
	 * @param {*} data
	 * @param {*} total
	 * @param {*} page
	 * @param {*} limit
	 */
	pageData(data: Pager, message?: string, code?: number): HttpPageTemplate {
		return {
			code: code || ResponseCode.SUCCESS,
			data: data,
			message: message || Message.SUCCESS
		}
	}


	/**
	 * 请求操作失败
	 * @param message
	 * @param code
	 * @param data
	 */
	failed(nonce: number, message?: string, code?: number): HttpResTemplate {
		return {
            nonce: nonce,
			message: message || Message.FAILED,
			code: code || ResponseCode.FAILED
		}
	}
}

export const HttpResult = new HttpResultClass()
